package com.atguigu.tingshu.user.stragegy.impl;

import cn.hutool.core.date.DateUtil;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.mapper.UserInfoMapper;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.user.UserVipService;
import com.atguigu.tingshu.model.user.VipServiceConfig;
import com.atguigu.tingshu.user.mapper.UserVipServiceMapper;
import com.atguigu.tingshu.user.mapper.VipServiceConfigMapper;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.user.stragegy.UpdateUserPaidRecordStrategy;
import com.atguigu.tingshu.vo.user.UserPaidRecordVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * UserVipServiceStrategy 会员购买
 *
 * @author Petrichor
 * @version 2024/01/08 23:01
 **/
@Service
public class UserVipServiceStrategy implements UpdateUserPaidRecordStrategy {
    private final UserVipServiceMapper userVipServiceMapper;
    private final VipServiceConfigMapper vipServiceConfigMapper;
    private final UserInfoMapper userInfoMapper;
    private final UserInfoService userInfoService;
    @Autowired
    public UserVipServiceStrategy(UserVipServiceMapper userVipServiceMapper, VipServiceConfigMapper vipServiceConfigMapper, UserInfoMapper userInfoMapper, UserInfoService userInfoService) {
        this.userVipServiceMapper = userVipServiceMapper;
        this.vipServiceConfigMapper = vipServiceConfigMapper;
        this.userInfoMapper = userInfoMapper;
        this.userInfoService = userInfoService;
    }

    @Override
    public void updateUserPaidRecord(UserPaidRecordVo userPaidRecordVo) {
        //防止重复消费
        LambdaQueryWrapper<UserVipService> lambdaQueryWrapper = new LambdaQueryWrapper<>() ;
        lambdaQueryWrapper.eq(UserVipService::getOrderNo, userPaidRecordVo.getOrderNo()) ;
        long count = userVipServiceMapper.selectCount(lambdaQueryWrapper);
        if (count > 0) {
            return;
        }
        // 获取vip配置信息
        Long itemId = userPaidRecordVo.getItemIdList().get(0);
        VipServiceConfig vipServiceConfig = vipServiceConfigMapper.selectById(itemId);

        // 构建要保存的交易记录对象
        UserVipService userVipService = new UserVipService();

        userVipService.setUserId(userPaidRecordVo.getUserId());
        userVipService.setOrderNo(userPaidRecordVo.getOrderNo());

        userVipService.setStartTime(new Date());
        userVipService.setExpireTime(new Date());
        //  获取当前系统时间
        Date startTime = new Date() ;

        //  vip 续期：
        //  获取用户信息
        UserInfo userInfo = userInfoMapper.selectById(userPaidRecordVo.getUserId());
        Integer isVip = userInfo.getIsVip();
        // 当前用户属于vip 并 用户的过期时间大于当前系统时间，则需要重新计算vip 的过期时间。
        if (isVip == 1 && userInfo.getVipExpireTime().after(new Date())){
            startTime = userInfo.getVipExpireTime();
        }
        //  获取过期时间
//        DateTime expireTime = DateUtil.offsetMonth(startTime, vipServiceConfig.getServiceMonth());
        Date expireTime = new DateTime(startTime).plusMonths(vipServiceConfig.getServiceMonth()).toDate();

        userVipService.setStartTime(new Date());

        userVipService.setExpireTime(expireTime);
        // 保存vip交易信息
        userVipServiceMapper.insert(userVipService);

        //更新用户vip信息
        userInfo.setIsVip(1);
        userInfo.setVipExpireTime(expireTime);
        userInfoService.updateById(userInfo);
    }

    @Override
    public String getItemType() {
        // 1003
        return SystemConstant.ORDER_ITEM_TYPE_VIP;
    }
}


